//
//  ZMNavController.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/22.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMNavController: UINavigationController ,UIGestureRecognizerDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置返回边缘手势
        self.interactivePopGestureRecognizer?.delegate = self
        
    }
    
    
//    override func popViewController(animated: Bool) -> UIViewController? {
//       let vc =  super.popViewController(animated: true)
//        print(viewControllers.count)
//      
//       print(viewControllers)
//        
//        return vc
//    }
//    

    //重写导航控制器的push方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        //跳入非跟控制器的时候隐藏底部导航
        if viewControllers.count > 0{
            
            viewController.hidesBottomBarWhenPushed = true
        }
        
        
        super.pushViewController(viewController, animated: true)
        
        if viewControllers.count > 1 {
            //非根控制器的时候,设置返回按钮
            var titlestring = ""
            if viewControllers.count == 2{
                titlestring = viewControllers.first!.title!
            }else {
                titlestring = "返回"
            }
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: titlestring, imageName: "navigationbar_back_withtext", target: self, action: #selector(backBarButtonClick))
            viewController.navigationItem.title = "当前显示的是第\(viewControllers.count)级控制器"
            
            viewController.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName:UIFont.systemFont(ofSize: 12),NSForegroundColorAttributeName:UIColor.orange]
            
            
        }
        
        
    }
    
    //返回按钮单击事件
     @objc private func backBarButtonClick(){
    
      self.popViewController(animated: true)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    //MARK:- 边缘手势代理方法
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        
        if viewControllers.count==1 {
            return false
        }
        return true
        
    }

  
}
